import request from "@/utils/request";
import type { ApiResponse } from "@/types";

export interface CategoryDTO {
  id: number;
  name: string;
  description?: string;
  icon?: string;
  color?: string;
  sort: number;
  isActive: boolean;
  createTime: string;
  updateTime: string;
  postCount: number;
}

export interface CategoryCreateRequest {
  name: string;
  description?: string;
  icon?: string;
  color?: string;
  sort?: number;
  isActive?: boolean;
}

/**
 * 获取分类列表
 */
export function getCategories(): Promise<CategoryDTO[]> {
  return request({
    url: "/categories",
    method: "get",
  });
}

/**
 * 创建分类
 */
export function createCategory(data: CategoryCreateRequest): Promise<CategoryDTO> {
  return request({
    url: "/categories",
    method: "post",
    data,
  });
}

/**
 * 更新分类
 */
export function updateCategory(
  categoryId: number,
  data: CategoryCreateRequest
): Promise<CategoryDTO> {
  return request({
    url: `/categories/${categoryId}`,
    method: "put",
    data,
  });
}

/**
 * 删除分类
 */
export function deleteCategory(categoryId: number): Promise<ApiResponse> {
  return request({
    url: `/categories/${categoryId}`,
    method: "delete",
  });
}

/**
 * 获取分类详情
 */
export function getCategoryById(categoryId: number): Promise<CategoryDTO> {
  return request({
    url: `/categories/${categoryId}`,
    method: "get",
  });
}
